package com.starblink.search.category.ui.newlist

import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.FragmentUtils
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.extension.navigationTo
import com.starblink.basic.route.RoutePage
import com.starblink.search.R
import com.starblink.search.category.data.CategoryModel
import com.starblink.search.category.ui.newlist.cell.LeveMainCell
import com.starblink.search.databinding.ActivityCategoryNewListBinding
import com.starblink.search.databinding.ItemMainCategoryBinding

@Route(path = RoutePage.Search.PAGE_GOODS_CATEGORY)
class CategoryNewListActivity :
    BaseTVMActivity<ActivityCategoryNewListBinding, NewListVM>() {

    @JvmField
    @Autowired(name = RoutePage.ARG.ID)
    var initCategoryId: String? = null
    override fun initViewBinding(): ActivityCategoryNewListBinding =
        ActivityCategoryNewListBinding.inflate(layoutInflater)

    var selectPos = 0

    val adapter: MutableAdapter by lazyOnNone {
        MutableAdapter(viewModel.contentList).apply {
            addVhDelegate {
                LeveMainCell(
                    ItemMainCategoryBinding.inflate(layoutInflater, it, false),
                ) { model, pos ->
                    var c1: CategoryModel = viewModel.contentList[selectPos]
                    c1.expand = false
                    selectPos = pos

                    model.expand = true
                    adapter.notifyDataSetChanged()
                    chooseListItem(pos, model)
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        CategoryConfig.initData()
        setStatusBarColor(Color.WHITE)
        viewModel.initCategoryId = initCategoryId
        viewBinding.titleBar.leftView.setOnClickListener {
            popSelf()
        }

        viewBinding.titleBar.rightView.setOnClickListener {
            navigationTo(RoutePage.Search.PAGE_SEARCH) {
                extraOf(RoutePage.Search.ARG_SCENE to RoutePage.Search.SCENE.GOODS.value)
            }
        }

        viewBinding.recycler.layoutManager = LinearLayoutManager(this)
        viewBinding.recycler.adapter = adapter

        viewModel.loadInitData()

        viewModel.liveData.observe(this) {
            if (it.isNotEmpty()) {
                adapter.notifyDataSetChanged()
                initFragment()

                if (viewModel.initCategoryId?.isNotEmpty() == true
                    && viewModel.initExpandModel != null
                    && viewModel.initExpandIndex != null
                ) {//定位到具体的类目
                    selectPos = viewModel.initExpandIndex ?: 0
                    chooseListItem(viewModel.initExpandIndex!!, viewModel.initExpandModel!!)
                    viewBinding.recycler.scrollToPosition(selectPos)
                } else {
                    chooseListItem(0, viewModel.contentList[0])
                }
            }
        }
    }

    private fun initFragment() {
        val fragment: Fragment =
            CategoryChildrenFragment.newInstance(viewModel.contentList[0].source.id.toString())
        FragmentUtils.add(
            supportFragmentManager,
            fragment, R.id.mFrame
        )
    }

    ///    切换fragment
    private fun chooseListItem(position: Int, model: CategoryModel) {
        CategoryConfig.setCategoryId(model.source.id.toString())
    }


}